/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrt.utility;

import java.lang.reflect.Method;

/**
 * 
 * @author biggie
 * @version $Revision: 1.2 $ $Date: 2008/07/15 01:50:22 $
 */
public class DependHelper {
	
	public static Object instance(String className) throws Exception{
		Object obj = null;
		try {
			Class c = Class.forName(className);
			obj = c.newInstance();
		} catch (Exception e) {
			throw new Exception("[DependHelper : instance()] - "+e.getMessage());
		}
		return obj;
	}

	public static void dependOn(Object objSrc,String property,Object dependObj) throws Exception{
		try {
			Object[] setObj = new Object[1];
			Class[]  setCls = new Class[1];
			setObj[0] = dependObj;
			setCls[0] = dependObj.getClass();
			String propertyName = UtilHelper.getMethod(property,"set");
			Method[] methods = objSrc.getClass().getMethods();
			for(int i =0;i<methods.length;i++){
				Method method = methods[i];
				if(method.getName().equals(propertyName)){
					method.invoke(objSrc,setObj);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("[DependHelper : dependOn()] - "+e.getMessage());
		}
	}

	public static Object setValue(Object srcObj,Object toObj) throws Exception{
		try{
			if(srcObj == null || toObj == null){
				return null;
			}
			Method[] toMethods = toObj.getClass().getMethods();
			for(int i=0;i<toMethods.length;i++){
				Method toMethod = toMethods[i];
				if(toMethod.getName().startsWith("set")){
					Object[] setObj = new Object[1];
					setObj[0] = getValue(srcObj,toMethod.getName());
					if(setObj[0] != null){
						toMethod.invoke(toObj,setObj);
					}
					
				}
				
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("[DependHelper : setValue()] - "+e.getMessage());
		}
		return toObj;
	}

	public static Method getReturnMethod(Object srcObj,String property) throws Exception{
		try{
			if(srcObj == null || property == null){
				return null;
			}
			Method[] srcMethods = srcObj.getClass().getMethods();
			for(int i=0;i<srcMethods.length;i++){
				Method method = srcMethods[i];
				if(method.getName().equals(UtilHelper.getMethod(property,"get"))){
					return method;
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("[DependHelper : getValue()] - "+e.getMessage());
		}
		return null;
	}
	
	public static Object getValue(Object srcObj,String property) throws Exception{
		try{
			if(srcObj == null || property == null){
				return null;
			}
			Method[] srcMethods = srcObj.getClass().getMethods();
			for(int i=0;i<srcMethods.length;i++){
				Method method = srcMethods[i];
				if(method.getName().equals(UtilHelper.getMethod(property,"get"))){
					return method.invoke(srcObj,null);
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("[DependHelper : getValue()] - "+e.getMessage());
		}
		return null;
	}

}
